//
//  XWMenuListItemBaseTBCell.swift
//  XWHAOfflineFitting
//
//  Created by edy on 2023/12/28.
//

import UIKit

class XWMenuListItemBaseTBCell: XWHBaseTBCell {
    
    override func addSubViews() {
        super.addSubViews()
        
        let font = AppFont.harmonyOSSans(ofSize: 14, weight: .regular)
        
        let textColor = majorTextColor
        let textAlignment = NSTextAlignment.left
        
        titleLb.font = font
        titleLb.textColor = textColor
        titleLb.textAlignment = textAlignment
    }

    override func relayoutSubViews() {
        relayoutSubViewsForOnlyTitle()
    }
    
    func relayoutSubViewsForOnlyTitle() {
        iconView.snp.removeConstraints()
        titleLb.snp.remakeConstraints { make in
            make.center.equalToSuperview()
            make.height.lessThanOrEqualToSuperview()
            make.width.lessThanOrEqualToSuperview()
        }
    }
    
    func relayoutSubViewsForLeftTitleRightIcon(iconSize: CGFloat) {
        iconView.snp.makeConstraints { make in
            make.size.equalTo(iconSize)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().inset(14)
        }
        titleLb.snp.remakeConstraints { make in
            make.height.equalTo(24)
            make.right.equalTo(iconView.snp.left).offset(-4)
            make.left.equalToSuperview().inset(14)
            make.centerY.equalToSuperview()
        }
    }
    
    func relayoutSubViewsForLeftIconRightTitle(iconSize: CGFloat) {
        iconView.snp.makeConstraints { make in
            make.size.equalTo(iconSize)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().inset(14)
        }
        titleLb.snp.remakeConstraints { make in
            make.height.equalTo(24)
            make.left.equalTo(iconView.snp.right).offset(4)
            make.right.equalToSuperview().inset(14)
            make.centerY.equalToSuperview()
        }
    }
    
    func configAndRelayoutSubViews(itemInfo: XWMenuListItemModel) {
        let type = itemInfo.itemType
        titleLb.font = itemInfo.textFont
        titleLb.textAlignment = itemInfo.textAlignment

        switch type {
        case .onlyTitle:
            relayoutSubViewsForOnlyTitle()
            
        case .leftTitleRightIcon:
            relayoutSubViewsForLeftTitleRightIcon(iconSize: itemInfo.iconSize)
            
        case .leftIconRightTitle:
            relayoutSubViewsForLeftIconRightTitle(iconSize: itemInfo.iconSize)
        }
    }
    
    func update(info: XWMenuListItemModel) {
        configAndRelayoutSubViews(itemInfo: info)
        if info.isInvalid {
            titleLb.textColor = info.invalidTextColor
        } else if info.isSelected {
            titleLb.textColor = info.selectedTextColor
        } else {
            titleLb.textColor = info.normalTextColor
        }
        
        iconView.image = info.icon
        if let attrTitle = info.attrTitle {
            titleLb.attributedText = attrTitle
        } else {
            titleLb.text = info.title
        }
        if let dotColor = info.badgeDotColor {
            titleLb.pp.addDot(color: dotColor)
            titleLb.pp.setBadge(height: info.badgeDotSize)
            titleLb.pp.moveBadge(x: 1, y: 0)
        } else {
            titleLb.pp.hiddenBadge()
        }
    }

}
